<?php
/**
 * class App_Model_Review
 *
 * @author kim
 */
class App_Model_Review extends App_Model_Abstract
{
    // table name
    protected $_name = 'dine2_review';

    // review status
    const STATUS_PENDING    = 1;
    const STATUS_APPROVED   = 2;
    const STATUS_DELETED    = 3;

    /**
     * get reviews by user id
     *
     * @param int $userId
     * @return array
     */
    public function getReviewsByUserId($userId)
    {
        $db = $this->getTable()->getAdapter();
        $query = "SELECT * FROM `dine2_review` WHERE `user_id` = '$userId' AND `status` = " . self::STATUS_APPROVED . " ORDER BY `rating` ASC";
        $reviews = $db->fetchAll($query);
        return $reviews;
    }

    /**
     * get reviews order by date created_at
     *
     * @param int $limit
     * @return array
     */
    public function getReviews($limit = 0)
    {
        $db = $this->getTable()->getAdapter();
        $query = "SELECT `dine2_review`.*, `dine2_user`.`firstname`, `dine2_user`.`lastname`, `dine2_place`.`name` as place_name "
               . "FROM `dine2_review` "
               . "LEFT JOIN `dine2_user` ON `dine2_review`.`user_id` = `dine2_user`.`id` "
               . "LEFT JOIN `dine2_place` ON `dine2_review`.`place_id` = `dine2_place`.`id` "
               . "WHERE `dine2_review`.`status` = " . self::STATUS_APPROVED . " ORDER BY `dine2_review`.`created_at` DESC";
        $query .= $limit ? " LIMIT $limit" : '';
        $reviews = $db->fetchAll($query);
        return $reviews;
    }

}
